package com.suray.wcs.service.thread;

import com.suray.basic.wcs.path.core.Node;
import com.suray.basic.wcs.utils.Action;
import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.wcs.res.enums.RgvStatus;
import com.suray.wcs.service.enums.NotificationTypeEnum;
import com.suray.wcs.service.enums.TaskRgvProgress;
import com.suray.wcs.service.enums.TaskType;
import com.suray.wcs.service.pojo.Rgv;
import com.suray.wcs.service.rescontact.ActionHandle;
import com.suray.wcs.service.scheduling.RgvTaskTimer;
import com.suray.wcs.service.system.entity.NodeDB;
import com.suray.wcs.service.system.entity.TaskDB;
import com.suray.wcs.service.system.service.impl.NodeDBServiceImpl;
import com.suray.wcs.service.system.service.impl.TaskDBServiceImpl;
import com.suray.wcs.service.util.WcsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Collections;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class ChargeErrorTimer extends BaseRgvThread{
    private Logger log = LoggerFactory.getLogger(this.getClass());
    private final TaskDBServiceImpl taskDBService = SpringContextHolder.getBean(TaskDBServiceImpl.class);
    private final NodeDBServiceImpl nodeDBService = SpringContextHolder.getBean(NodeDBServiceImpl.class);
    public ChargeErrorTimer(Rgv rgv) {
        super(rgv.getRgvName() + "充电异常处理线程", rgv);
    }

    public void start() {
        this.log.info("执行异常现场开启》》》》》》》》");
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        executorService.scheduleWithFixedDelay(() -> {
            Thread.currentThread().setName(parentRgv.getRgvName() + "充电异常检测");
            if (this.parentRgv.getRgvStatus() == RgvStatus.CHARGING && this.parentRgv.getBattery() != null) {
                int battery = this.parentRgv.getBattery();
                try {
                    Thread.sleep(60000L);
                } catch (InterruptedException var3) {
                    var3.printStackTrace();
                }
                if (this.parentRgv.getCurTask() == null && this.parentRgv.getRgvStatus() == RgvStatus.CHARGING && this.parentRgv.getBattery() != null && battery > this.parentRgv.getBattery()) {
                    this.log.error("{}充电中，但是电量仍然在下降，请检查充电桩是否开启", this.parentRgv.getRgvName());
                    WcsUtil.notification(this.parentRgv.getRgvName() + "充电中，但是电量仍然在下降，请检查充电桩是否开启", NotificationTypeEnum.WARN);
                    try {
                        // 发送充电结束命令和后退任务(先生成任务)
                        ActionHandle.actionHandle(null, null, this.parentRgv, null, null, Action.ENDCHARGE);
                        TaskDB task = new TaskDB();
                        NodeDB nodedb = nodeDBService.getNodeDB(parentRgv.getCurLocation());
                        Node backNode = RgvTaskTimer.getBackNode(nodedb);
                        task.setEndNode(backNode);
                        task.setTaskType(TaskType.MOVE_UNLOADED);
                        task.setProcessingRgvNo(parentRgv.getRgvDB().getRgvNo());
                        task.setRgvProgress(TaskRgvProgress.ASSIGNED_TO_RGV);

                        //防止小车去执行其它任务
                        task.setPriorityCode(50000);

                        taskDBService.insertTask(Collections.singletonList(task));
                    } catch (RuntimeException e) {
                        log.error("充电桩可行驶方向不正确，请手动添加后退方案：{}", e.getMessage(), e);
                    } catch (Exception e) {
                        log.error("充电异常处理线程异常：{}", e.getMessage(), e);
                    }
                }
            }
        }, 0L, 1L, TimeUnit.SECONDS);
        this.log.info(super.parentRgv.getRgvDB().getRgvNo() + "号车充电异常检测线程启动");
    }
}
